﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace LPNRTool
{
    class WriteLog
    {
        ConfigHelper config = new ConfigHelper();

        public void WriteLogFile(string input)
        {
            int Max_Log = 1024 * 1024 * 10;
            string ipaddress = LPNRTool.GetManchineIPAddress;
            string LogPath = ConfigHelper.GetConnectionStringConfig("logpath");//读取配置文件内容
            string filename = ipaddress + " " + ConfigHelper.GetConnectionStringConfig("logfilename");
            string filepathname = System.IO.Path.Combine(LogPath, filename);

            if (!Directory.Exists(LogPath))
            {
                System.IO.Directory.CreateDirectory(LogPath);
            }

            FileInfo finfo = new FileInfo(filepathname);
            if (finfo.Exists && finfo.Length > Max_Log)
            {
                string NewFileName = LogPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + " " + ipaddress + "Logbak.log";
                FileInfo NewFile = new FileInfo(NewFileName);
                if (NewFile.Exists)
                {
                    NewFile.Delete();
                }
                File.Move(filepathname, NewFileName);
            }

            using (System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))
            {
                //fs.Flush();
                string logLine = System.String.Format("{0:G}: {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");

                //byte[] msg = new byte[fs.Length];
                byte[] msg = new byte[1024];
                msg = Encoding.UTF8.GetBytes(logLine);
                fs.Write(msg, 0, msg.Length);
                fs.Close();
            }
        }

        public static void WriteLogstatic(string input)
        {
            try
            {
                int Max_Log = 1024 * 1024 * 10;
                string ipaddress = LPNRTool.GetManchineIPAddress;
                string LogPath = ConfigHelper.GetConnectionStringConfig("logpath");//读取配置文件内容
                string filename = ipaddress + " " + ConfigHelper.GetConnectionStringConfig("logfilename");
                string filepathname = System.IO.Path.Combine(LogPath, filename);


                if (!Directory.Exists(LogPath))
                {
                    System.IO.Directory.CreateDirectory(LogPath);
                }

                FileInfo finfo = new FileInfo(filepathname);
                if (finfo.Exists && finfo.Length > Max_Log)
                {
                    string NewFileName = LogPath + "\\" + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + "Logbak.log";
                    FileInfo NewFile = new FileInfo(NewFileName);
                    if (NewFile.Exists)
                    {
                        NewFile.Delete();
                    }
                    File.Move(filepathname, NewFileName);
                }

                System.IO.FileStream fs = new System.IO.FileStream(filepathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite);

                //fs.Flush();
                string logLine = System.String.Format("{0:G}: {1}", System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff"), input + "\r\n");

                //byte[] msg = new byte[fs.Length];
                byte[] msg = new byte[1024];
                msg = Encoding.UTF8.GetBytes(logLine);
                fs.Write(msg, 0, msg.Length);
                fs.Close();
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message.ToString());
            }
        }


        /// <summary>
        /// 写入抓拍机ip地址 至caramList
        /// </summary>
        /// <param name="str"></param>
        public void writeIpList(string str)
        {
            string pathname = writeorreadIpList();

            using (FileStream wfs = new FileStream(pathname, System.IO.FileMode.Append, System.IO.FileAccess.Write, FileShare.ReadWrite))
            {
                wfs.Flush();
                string content = str + "\n";
                byte[] msg = new byte[str.Length];
                msg = Encoding.UTF8.GetBytes(content);
                wfs.Write(msg, 0, msg.Length);
                wfs.Close();
            }
        }

        public string returnipList = "";
        public string readIpList()
        {
            string str = "";
            string pathname = writeorreadIpList();
            using (FileStream fs = new FileStream(pathname, FileMode.OpenOrCreate, FileAccess.Read))
            {
                StreamReader sr = new StreamReader(fs);
                while ((str = sr.ReadLine()) != null)
                {
                    returnipList += "\n" + str;
                }
                sr.Close();
                fs.Close();
            }
            return returnipList;
        }


        public string writeorreadIpList()
        {
            string path = AppDomain.CurrentDomain.BaseDirectory;
            string name = "camera_ip.lst";
            string pathname = Path.Combine(path, name);

            return pathname;
        }

    }
}
